package com.tehui.utils.ui;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.tehui.app.MyApplication;

/**
 * Created by jasmine on 2017/2/28.
 */

public class UiUtils {

    private static PackageInfo packageInfo;

    //获取全局Context
    public static Context getContext() {
        return MyApplication.applicationContext;
    }

    public static Handler getMainThreadHandler() {
        return MyApplication.handler;
    }

    public static int getMainThreadId() {
        return MyApplication.mainThreadId;
    }

    public static void toast(final String msg) {
        //Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //判断是否是主线程
    public static boolean isRunOnUiThread() {
        int mainThreadId = getMainThreadId();
        int currentThreadId = android.os.Process.myTid();
        return mainThreadId == currentThreadId;
    }

    //保证一段代码一定是在主线程中运行的
    public static void runOnUiThread(Runnable r) {
        if (isRunOnUiThread()) {
            r.run();
        } else {
            //子线程
            getMainThreadHandler().post(r);//将r丢到主线程的消息队列
        }
    }

    public static boolean isNetworkAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) getContext().getSystemService(Context
                .CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }

    public static void MoveToPosition(LinearLayoutManager manager) {
        manager.scrollToPositionWithOffset(0, 0);
        manager.setStackFromEnd(false);
    }

    public static String getVersionName() {
        PackageManager pm = getContext().getPackageManager();
        try {
            packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo.versionName;
    }

    public static int getVersionCode() {
        PackageManager pm = getContext().getPackageManager();
        try {
            packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo.versionCode;
    }

    public static String getIMEI(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        return imei;
    }
}
